package com.xm.dao.background;

import com.xm.entity.LogisticsStation;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 物流站Dao
 * @author 康文程
 * @date 2024-1-11 上午 11:10
 */
public class LogisticsStationDao extends BaseUtil {
    /**
     * 根据物流公司id查询物流站
     *
     * @param logisticsCompaniesId 物流公司编号
     * @return 物流站列表
     * @throws SqlErrorException SQL异常
     */
    public List<LogisticsStation> getLogisticsStationsByLogisticsCompanyId(int logisticsCompaniesId) throws SqlErrorException {
        @Language("sql")
        String sql = "SELECT * FROM logistics_station WHERE logistics_companies_id = ?";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql, logisticsCompaniesId);
            List<LogisticsStation> logisticsStationList = new ArrayList<>();
            while (resultSet.next()) {
                LogisticsStation logisticsStation = new LogisticsStation();
                logisticsStation.setLogisticsStationId(resultSet.getInt("logistics_station_id"));
                logisticsStation.setLogisticsStationName(resultSet.getString("logistics_station_name"));
                logisticsStation.setLogisticsStationProvince(resultSet.getString("logistics_station_province"));
                logisticsStation.setLogisticsStationCity(resultSet.getString("logistics_station_city"));
                logisticsStation.setLogisticsStationDistrict(resultSet.getString("logistics_station_district"));
                logisticsStation.setLogisticsStationLongitude(resultSet.getString("logistics_station_longitude"));
                logisticsStation.setLogisticsStationLatitude(resultSet.getString("logistics_station_latitude"));
                logisticsStation.setLogisticsStationDeliveryman(resultSet.getString("logistics_station_deliveryman"));
                logisticsStation.setLogisticsStationPhone(resultSet.getString("logistics_station_phone"));
                logisticsStation.setLogisticsCompaniesId(resultSet.getInt("logistics_companies_id"));
                logisticsStationList.add(logisticsStation);
            }
            return logisticsStationList;
        } catch (SQLException e) {
            throw new SqlErrorException("根据物流公司id查询物流站失败", e);
        } finally {
            close(resultSet);
        }
    }

    /**
     * 根据物流站ID删除物流站
     *
     * @param logisticsStationId 物流站ID
     * @throws SqlErrorException SQL异常
     */
    public void deleteLogisticsStationById(int logisticsStationId) throws SqlErrorException {
        @Language("sql")
        String sql = "DELETE FROM logistics_station WHERE logistics_station_id = ?";
        try {
            executeUpdate(sql, logisticsStationId);
        } catch (SQLException e) {
            throw new SqlErrorException("根据物流站ID删除物流站失败", e);
        }
    }
    /**
     * 新增物流站
     *
     * @param logisticsStation 要新增的物流站信息
     * @throws SqlErrorException SQL异常
     */
    public void addLogisticsStation(LogisticsStation logisticsStation) throws SqlErrorException {
        @Language("sql")
        String sql = "INSERT INTO logistics_station (logistics_station_name, logistics_station_province, " +
                "logistics_station_city, logistics_station_district, logistics_station_longitude, " +
                "logistics_station_latitude, logistics_station_deliveryman, logistics_station_phone, " +
                "logistics_companies_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] params = {
                logisticsStation.getLogisticsStationName(),
                logisticsStation.getLogisticsStationProvince(),
                logisticsStation.getLogisticsStationCity(),
                logisticsStation.getLogisticsStationDistrict(),
                logisticsStation.getLogisticsStationLongitude(),
                logisticsStation.getLogisticsStationLatitude(),
                logisticsStation.getLogisticsStationDeliveryman(),
                logisticsStation.getLogisticsStationPhone(),
                logisticsStation.getLogisticsCompaniesId()
        };
        try {
            executeUpdate(sql, params);
        } catch (SQLException e) {
            throw new SqlErrorException("新增物流站失败", e);
        }
    }
}
